//
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <locale>

using namespace std;

int get_sock_tc(int& sock, const char* server_ip, int port) {
    // 设置UTF-8编码环境
    locale::global(locale("en_US.utf-8"));
    wcout.imbue(locale());
    struct sockaddr_in serv_addr;
    // 创建socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    // 转换IP地址
    if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0) {
        std::cerr << "Invalid address/ Address not supported" << std::endl;
        return -1;
    }
    // 连接服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection failed" << std::endl;
        return -1;
    }
    return 0;
}

int send_msg_tc(const int& sock, const std::string& message, std::string& response) {
    // 发送数据
    send(sock, message.c_str(), message.length(), 0);
    std::cout << "Message sent..." << std::endl;
    // 接收响应
    char buffer[1024] = {0};
    int bytes_received = read(sock, buffer, 1024);
    if (bytes_received > 0) {
        // string response(buffer, bytes_received);
        response.clear();
        response.append(buffer, bytes_received);
        std::cout << "Server response: " << response << std::endl;
    } else {
        std::cerr << "Connection closed by server" << std::endl;
        // break;
    }
    return 0;
}

/**
 * 完整发送TCP消息，包括：建立连接=》发送消息=》接收响应=》关闭连接
*/
int TC_send_msg(const char* server_ip, int PORT, const std::string& message, std::string& response) {
    // 设置UTF-8编码环境
    locale::global(locale("en_US.utf-8"));
    wcout.imbue(locale());

    int sock = 0;
    struct sockaddr_in serv_addr;
    // const char* server_ip = "192.168.1.115";
    // const int PORT = 8080;

    // 创建socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 转换IP地址
    if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0) {
        std::cerr << "Invalid address/ Address not supported" << std::endl;
        return -1;
    }

    // 连接服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection failed" << std::endl;
        return -1;
    }

    std::cout << "Connected to server. Enter 'exit' to quit." << std::endl;

    // while (true) {
    // std::string message = "Hello World 版本：v0.0.1";
    // std::cout << "Enter message: ";
    // getline(std::cin, message);
    // if (message == "exit") break;
    // 发送数据
    send(sock, message.c_str(), message.length(), 0);
    std::cout << "Message sent..." << std::endl;
    // 接收响应
    char buffer[1024] = {0};
    int bytes_received = read(sock, buffer, 1024);
    if (bytes_received > 0) {
        // string response(buffer, bytes_received);
        response.clear();
        response.append(buffer, bytes_received);
        std::cout << "Server response: " << response << std::endl;
    } else {
        std::cerr << "Connection closed by server" << std::endl;
        // break;
    }
    close(sock);
    return 0;
}

int tcp_client_send(const std::string& message, std::string& response) {
    // 设置UTF-8编码环境
    locale::global(locale("en_US.utf-8"));
    wcout.imbue(locale());

    int sock = 0;
    struct sockaddr_in serv_addr;
    const char* server_ip = "192.168.1.115";
    const int PORT = 8080;

    // 创建socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 转换IP地址
    if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0) {
        std::cerr << "Invalid address/ Address not supported" << std::endl;
        return -1;
    }

    // 连接服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection failed" << std::endl;
        return -1;
    }

    std::cout << "Connected to server. Enter 'exit' to quit." << std::endl;

    // while (true) {
    // std::string message = "Hello World 版本：v0.0.1";
    // std::cout << "Enter message: ";
    // getline(std::cin, message);
    // if (message == "exit") break;
    // 发送数据
    send(sock, message.c_str(), message.length(), 0);
    std::cout << "Message sent..." << std::endl;
    // 接收响应
    char buffer[1024] = {0};
    int bytes_received = read(sock, buffer, 1024);
    if (bytes_received > 0) {
        // string response(buffer, bytes_received);
        response.clear();
        response.append(buffer, bytes_received);
        std::cout << "Server response: " << response << std::endl;
    } else {
        std::cerr << "Connection closed by server" << std::endl;
        // break;
    }
    close(sock);
    return 0;
}